package com.kingtone.rongji.network;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpStatus;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.kingtone.rongji.biz.CommonMessage;
import com.kingtone.rongji.biz.ProductDetail;
import com.kingtone.rongji.biz.ProductGroup;
import com.kingtone.rongji.biz.ProductImageSet;
import com.kingtone.rongji.tool.AppRestClient;
import com.kingtone.rongji.tool.RongJiRestClient;
import com.kingtone.rongji.tool.RongJiUtils;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ProductDetailRequest extends CommonRequest {

	public void findGallery(String productId, final IRequestHandler restClient) {
		RequestParams params = new RequestParams();
		params.put("productId", productId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(RongJiRestClient.PRODUCT_DETAIL_IMAGES_URL, params, new ProductImageHandler(
				restClient));
	}
	
	public void findDetail(String productId, final IRequestHandler restClient) {
		RequestParams params = new RequestParams();
		params.put("productId", productId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(RongJiRestClient.PRODUCT_DETAIL_URL, params, new ProductDetailHandler(
				restClient, true));
	}

	public void findDetailGroup(String productId, final IRequestHandler restClient) {
		RequestParams params = new RequestParams();
		params.put("productId", productId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(RongJiRestClient.PRODUCT_GROUP_URL, params, new ProductGroupHandler(
				restClient));
	}
	
	public void addFavorites(String productId, String userId, double unitPrice, final IRequestHandler restClient) {
		RequestParams params = new RequestParams();
		params.put("user", userId);
		params.put("unitPrice", unitPrice);
		params.put("productId", productId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(RongJiRestClient.PRODUCT_ADD_FAV_URL, params, this.createCommonHandler(restClient, false));
	}
	
	public void checkFavorite(String productId, String userId, final IRequestHandler restClient) {
		RequestParams params = new RequestParams();
		params.put("userId", userId);
		params.put("productId", productId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(RongJiRestClient.PRODUCT_CHECK_FAV_URL, params, new ProductFavHandler(restClient));
	}
	
	class ProductImageHandler extends BaseJsonHttpResponseHandler {

		private IRequestHandler restClient;

		public ProductImageHandler(IRequestHandler restClient) {
			super();
			this.restClient = restClient;
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, Throwable throwable,
				String arg3, Object arg4) {
			// TODO Auto-generated method stub
			if (null != throwable) {
				throwable.printStackTrace();
			}
		}

		@Override
		public void onSuccess(int statusCode, Header[] arg1,
				String responseString, Object arg3) {
			// TODO Auto-generated method stub
			if (statusCode == HttpStatus.SC_OK) {
				try {
					ProductImageSet imageSet = parseResponse(responseString, false);
					if (imageSet != null) {
						restClient.onBussinessSuccess(imageSet);
						return;
					}
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		@Override
		protected ProductImageSet parseResponse(String rawJsonData,
				boolean isFailure) throws Throwable {
			// TODO Auto-generated method stub
			Gson gson = new Gson();
			JsonParser parser = new JsonParser();
			JsonElement elment = parser.parse(rawJsonData);
			JsonArray jsonArray = elment.getAsJsonArray();
			for (JsonElement jsonObj: jsonArray) {
				ProductImageSet imageSet = gson.fromJson(jsonObj, ProductImageSet.class);
				return imageSet;
			}
			return null;
		}
	}
	
	class ProductFavHandler extends BaseJsonHttpResponseHandler {

		private IRequestHandler restClient;

		public ProductFavHandler(IRequestHandler restClient) {
			super();
			this.restClient = restClient;
		}
		
		@Override
		public void onFailure(int statusCode, Header[] header, Throwable throwable,
				String arg3, Object arg4) {
			if (null != throwable) {
				throwable.printStackTrace();
			}
			restClient.onNetworkFailure();
		}

		@Override
		public void onSuccess(int statusCode, Header[] header, String responseString,
				Object arg3) {
			if (statusCode == HttpStatus.SC_OK) 
			{
				try {
					CommonMessage common = (CommonMessage) parseResponse(responseString, false);
					if (common != null) {
						if(common.getStatus())
						{
							restClient.onBussinessSuccess(common);
						}
						return;
					}
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		@Override
		protected Object parseResponse(String rawJsonData, boolean isFailure)
				throws Throwable {
			// TODO Auto-generated method stub
			Gson gson = new Gson();
			JsonParser parser = new JsonParser();
			JsonElement elment = parser.parse(rawJsonData);
			CommonMessage common = gson.fromJson(elment, CommonMessage.class);
			return common;
		}
	}
	
	class ProductGroupHandler extends BaseJsonHttpResponseHandler {

		private IRequestHandler restClient;

		public ProductGroupHandler(IRequestHandler restClient) {
			super();
			this.restClient = restClient;
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, Throwable throwable,
				String arg3, Object arg4) {
			// TODO Auto-generated method stub
			if (null != throwable) {
				throwable.printStackTrace();
			}
			restClient.onNetworkFailure();
		}

		@Override
		public void onSuccess(int statusCode, Header[] arg1,
				String responseString, Object arg3) {
			// TODO Auto-generated method stub
			if (statusCode == HttpStatus.SC_OK) {
				try {
					List<ProductGroup> groups = parseResponse(responseString, false);
					if (groups != null) {
						restClient.onBussinessSuccess(groups);
						return;
					}
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		@Override
		protected List<ProductGroup> parseResponse(String rawJsonData,
				boolean isFailure) throws Throwable {
			// TODO Auto-generated method stub
			Gson gson = new Gson();
			JsonParser parser = new JsonParser();
			JsonElement elment = parser.parse(rawJsonData);
			JsonArray jsonArray = elment.getAsJsonArray();
			List<ProductGroup> groups = new ArrayList<ProductGroup>();
			for (JsonElement jsonObj: jsonArray) {
				ProductGroup group = gson.fromJson(jsonObj, ProductGroup.class);
				groups.add(group);
			}
			return groups;
		}
	}
	
	class ProductDetailHandler extends BaseJsonHttpResponseHandler {

		private IRequestHandler restClient;
		
		private boolean showProgress;

		public ProductDetailHandler(IRequestHandler restClient, boolean showProgress) {
			super();
			this.restClient = restClient;
			this.showProgress = showProgress;
		}

		@Override
		public void onStart() {
			if(showProgress)
			{
				RongJiUtils.showEmptyProgress(restClient.getContext());
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, Throwable throwable,
				String arg3, Object arg4) {
			// TODO Auto-generated method stub
			if (null != throwable) {
				throwable.printStackTrace();
			}
			if(showProgress)
			{
				RongJiUtils.dismissProgress();
			}
			restClient.onNetworkFailure();
		}

		@Override
		public void onSuccess(int statusCode, Header[] arg1,
				String responseString, Object arg3) {
			// TODO Auto-generated method stub
			if(showProgress)
			{
				RongJiUtils.dismissProgress();
			}
			
			if (statusCode == HttpStatus.SC_OK) {
				try {
					ProductDetail imageSet = parseResponse(responseString, false);
					if (imageSet != null) {
						restClient.onBussinessSuccess(imageSet);
						return;
					}
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		@Override
		protected ProductDetail parseResponse(String rawJsonData,
				boolean isFailure) throws Throwable {
			// TODO Auto-generated method stub
			Gson gson = new Gson();
			JsonParser parser = new JsonParser();
			JsonElement elment = parser.parse(rawJsonData);
			JsonArray jsonArray = elment.getAsJsonArray();
			for (JsonElement jsonObj: jsonArray) {
				ProductDetail detail = gson.fromJson(jsonObj, ProductDetail.class);
				return detail;
			}
			return null;
		}

	}
}
